using Microsoft.SqlServer.Tools.HostingServices;
using System;
using System.Net;
using System.Xml.XPath;

namespace Microsoft.SqlServer.Management.SqlManagerUI.Scripting
{
	internal class WebServicePublishOptions
	{
		private string _webServiceVersion = string.Empty;

		private ulong _maxRequestLength;

		private string _serverVersion = string.Empty;

		private string _databaseCollation = string.Empty;

		private PublishService _webService;

		internal bool HasDatabaseCollation
		{
			get
			{
				return !string.IsNullOrEmpty(this._databaseCollation);
			}
		}

		internal bool HasDatabaseServerVersion
		{
			get
			{
				return !string.IsNullOrEmpty(this._serverVersion) && this.SplitVersionString(this._serverVersion).Length == 3;
			}
		}

		internal ulong MaxRequestLength
		{
			get
			{
				return this._maxRequestLength;
			}
			set
			{
				this._maxRequestLength = value;
			}
		}

		internal string DatabaseServerVersionFull
		{
			get
			{
				if (!this.HasDatabaseServerVersion)
				{
					this.ThrowNoValueException("database server version");
				}
				return this._serverVersion;
			}
			set
			{
				this._serverVersion = value;
			}
		}

		internal int DatabaseServerVersionMajor
		{
			get
			{
				if (!this.HasDatabaseServerVersion)
				{
					this.ThrowNoValueException("database server version");
				}
				return int.Parse(this.SplitVersionString(this._serverVersion)[0]);
			}
		}

		internal string DatabaseCollation
		{
			get
			{
				if (!this.HasDatabaseCollation)
				{
					this.ThrowNoValueException("database collation");
				}
				return this._databaseCollation;
			}
			set
			{
				this._databaseCollation = value;
			}
		}

		internal WebServicePublishOptions()
		{
		}

		internal WebServicePublishOptions(PublishService webService, string databaseServerName, string databaseName, string databaseUserName, string databasePassword)
		{
			if (webService == null)
			{
				throw new ArgumentException("Web Service cannot be null.");
			}
			if (webService.Credentials == null)
			{
				throw new ArgumentException("Web Service credentials cannot be null.");
			}
			if (string.IsNullOrEmpty(webService.Url))
			{
				throw new ArgumentException("Web Service Url cannot be empty or null.");
			}
			this._webService = webService;
			this.Initialize(databaseServerName, databaseName, databaseUserName, databasePassword);
		}

		internal WebServicePublishOptions(Uri webServiceAddress, string webServiceUsername, string webServicePassword, string databaseServerName, string databaseName, string databaseUserName, string databasePassword)
		{
			this._webService = new PublishService();
			this._webService.Url = webServiceAddress.ToString();
			this._webService.Credentials = new NetworkCredential(webServiceUsername, webServicePassword);
			this._webService.CookieContainer = new CookieContainer();
			this.Initialize(databaseServerName, databaseName, databaseUserName, databasePassword);
		}

		private void Initialize(string databaseServerName, string databaseName, string databaseUserName, string databasePassword)
		{
			this._webService.BeginPublish(databaseServerName, databaseName, databaseUserName, databasePassword, false);
			XPathNavigator xPathNavigator = this._webService.GetServiceOptions().CreateNavigator();
			XPathNodeIterator xPathNodeIterator = xPathNavigator.Select("/options");
			XPathNavigator current = xPathNodeIterator.Current;
			XPathNodeIterator xPathNodeIterator2 = current.SelectDescendants(XPathNodeType.Element, false);
			string text = string.Empty;
			while (xPathNodeIterator2.MoveNext())
			{
				text = xPathNodeIterator2.Current.Value;
				string name;
				if ((name = xPathNodeIterator2.Current.Name) != null)
				{
					if (!(name == "sql_server_version"))
					{
						if (!(name == "database_collation"))
						{
							if (!(name == "max_request_length"))
							{
								if (name == "service_version")
								{
									this._webServiceVersion = text;
								}
							}
							else
							{
								this._maxRequestLength = ulong.Parse(text);
							}
						}
						else
						{
							this._databaseCollation = text;
						}
					}
					else
					{
						this._serverVersion = text;
					}
				}
			}
			if (this._maxRequestLength < 1uL)
			{
				throw new InvalidOperationException("The web service did not return a valid value for Maximum Request Length.");
			}
			this._webService.EndPublish();
		}

		private string[] SplitVersionString(string version)
		{
			return version.Split(new char[]
			{
				'.'
			});
		}

		private void ThrowNoValueException(string valueType)
		{
			throw new InvalidOperationException(string.Format("There is no current valid {0} value.", valueType));
		}
	}
}
